package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.bean.dto.VtuzxPage;
import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.bean.VtuzxArrayList;
import org.bson.Document;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 采购收货单Dao
 */
@Component
public class PurColDao extends VtuzxDaoImpl {
    /** 集合名称 */
    public static final String NAME = "t_pur_col";

    public PurColDao() {
        super(NAME);
    }

    public VtuzxPage getPageList(Document cond, boolean checkNoRfid, boolean isNoRfid, int pageIndex, int pageSize) {
        Document project = new Document("_id", 1);
        if (checkNoRfid) {
            project.append("notBindCount", new Document("$subtract", new VtuzxArrayList<>("$boxCount", "$bindBoxCount")));
        }
        List<Document> countDocList = new ArrayList<>();
        countDocList.add(new Document("$match", cond));
        if (checkNoRfid) {
            countDocList.add(new Document("$project", project));
            countDocList.add(new Document("$match", new Document("notBindCount", new Document((isNoRfid? "$ne": "$eq"), 0))));
        }
//        if (checkNoStockIn) {
//            countDocList.add(new Document("$match", new Document("")))
//        }
        countDocList.add(new Document("$group", new Document("_id", null).append("total", new Document("$sum", 1))));
        Iterator<Document> countItr = this.aggregate(countDocList);
        long total = 0;
        if (countItr.hasNext()) {
            Document totalDoc = countItr.next();
            total = VtuzxUtil.getLong(totalDoc, "total");
        }

        if (total == 0) {
            return new VtuzxPage(0, null);
        }
        List<Document> dataDocList = new ArrayList<>();
        dataDocList.add(new Document("$match", cond));
        if (checkNoRfid) {
            project.append("billNo", true);
            project.append("materialNo", true);
            project.append("materialName", true);
            project.append("materialSpec", true);
            project.append("batchNo", true);
            project.append("count", true);
            project.append("produceDate", true);
            project.append("exp", true);
            project.append("boxCount", true);
            project.append("bindBoxCount", true);
            project.append("createUserName", true);
            project.append("createTime", true);
            project.append("auditUserName", true);
            project.append("auditTime", true);
            project.append("status", true);
            dataDocList.add(new Document("$project", project));
            dataDocList.add(new Document("$match", new Document("notBindCount", new Document((isNoRfid? "$ne": "$eq"), 0))));
        }
        dataDocList.add(new Document("$sort", new Document("createTime", -1)));
        dataDocList.add(new Document("$skip", pageIndex * pageSize));
        dataDocList.add(new Document("$limit", pageSize));
        Iterator<Document> dataItr = this.aggregate(dataDocList);

        return new VtuzxPage(total, dataItr);
    }

}